package com.ctesi.service.zhaobiao.hunan;
import com.ctesi.utils.PutUtil;
import com.ctesi.utils.StringExtract;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.Random;
@Service
public class ZhuZhouProcessorService implements PageProcessor {
    private String bidType;
    private Site site;
    Random random = new Random();
    @Override
    public void process(Page page) {
        // 生成5000到10000之间的随机整数
        int randomTime = random.nextInt(10000 - 5000 + 1) + 5000;
        //设置请求头和等待时间
        this.site = Site.me()
                .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36")
                .setTimeOut(randomTime) // 设置超时时间为5秒到10秒之间的数
                .setRetryTimes(5); // 设置重试次数为 3 次
        site.addHeader("Connection", "keep-alive");


        //裂变url和内容url逻辑
        String currentUrl = page.getUrl().get();
        if (currentUrl.matches("http://www.zzzyjy.cn/016/016001/016001001/\\d\\.html")){
            //裂变url
            System.out.println("----------正在对裂变url进行处理----------------");
            for (int i = 1; i <= 13; i++) {
                String pubTime = page.getHtml().xpath(String.format("/html/body/div[2]/div[2]/div[2]/ul/li[%d]/span/text()",i)).toString();
                if (pubTime.contains("2023")){
                    page.addTargetRequest(String.format("/html/body/div[2]/div[2]/div[2]/ul/li[%d]/a/@href",i));
                    System.out.println("----------------处理成功-----------------------");
                }

            }
        }else {
            //内容抓取
            System.out.println("-----------正在进行内容抓取-------------");

            //招标内容主体信息
            String context = page.getHtml().css("div.content").toString();
            String bidContext = StringExtract.removeEmptyLines(StringExtract.removeTags(StringExtract.removeNbsp(context)));
            System.out.println(bidContext);

            //招标编号
            PutUtil.putField(page,bidContext,"\\[(.*?)\\].*?.HNZF","bidId");

            //招标类型
            if (currentUrl.contains("016001")){
                page.putField("bidType","房建市政");
            } else if (currentUrl.contains("016003")) {
                page.putField("bidType","交通工程");
            }else if (currentUrl.contains("4")){
                page.putField("bidType","水利工程");
            }

            //招标省份
            page.putField("bidProvince","湖南省");

            //招标标题
            PutUtil.putField(page,bidContext,"\\](.*?)\\.HNZF","bidTitle");

            //招标范围
            PutUtil.putField(page,bidContext,"招标范围：(.*?)\\n\\d","bidArange");



            //招标公告发布时间
            String pubtime = page.getHtml().xpath("/html/body/div[2]/div[2]/div[1]/span[1]/text()").toString();
            String bidPubTime = StringExtract.extractTextByRegex(pubtime,"信息时间：(.*?)\\】");
            page.putField("bidPubTime",bidPubTime);

            //备注：株洲市没有这个字段

            //数据创建时间

            //数据更新时间

            //招标url
            page.putField("bidUrl",currentUrl);

            //招标地区/建设地点
            PutUtil.putField(page,bidContext,"建设地点：.*?(.*?)\\n","bidArea");

            //招标人资格要求
            PutUtil.putField(page,bidContext,".{0,15}资质$","bidCredit");

            //评标办法
            PutUtil.putField(page,bidContext,"评标办法\\n.*?采用(.*?)\\。","bidJudgeMethod");



            //招标状态：株洲市没有这个字段

            //招标信息

            //招标单位
            PutUtil.putField(page,bidContext," 招[\\s]*标[\\s]*(人|单位)：(.*?)；","bidUnity");

            //招标联系人
            PutUtil.putField(page,bidContext,"10.联系方式\\n.*?招.*?标.*?人|单位：.*?\\n.*?\\n.*?联.*?系.*?人：.*?(.*?)\\n","bidName");

            //招标联系人电话
            PutUtil.putField(page,bidContext,"10.联系方式\\n.*?招.*?标.*?人|单位：.*?\\n.*?\\n.*?\\n.*?电.*?话：.*?(.*?)\\n","bidPhone");

            //招标单位地址
            PutUtil.putField(page,bidContext,"10.联系方式\\n.*?招.*?标.*?人|单位：.*?\\n.*?地.*?址：.*?(.*?)\\n","bidAddress");

            //代理机构信息

            //代理机构单位
            PutUtil.putField(page,bidContext,"招.*?标.*?代.*?理.*?机.*?构：(.*?)\\n","agencyUnity");

            //代理机构联系人
            PutUtil.putField(page,bidContext,"招.*?标.*?代.*?理.*?机.*?构：.*?\\n.*?\\n.*?联.*?系.*?人：(.*?)\\n","agencyName");

            //代理机构联系人电话
            PutUtil.putField(page,bidContext,"招.*?标.*?代.*?理.*?机.*?构：.*?\\n.*?\\n.*?\\n.*?电.*?话：(.*?)\\n","agencyPhone");


            //代理机构地址
            PutUtil.putField(page,bidContext,"招.*?标.*?代.*?理.*?机.*?构：.*?\\n.*?地.*?址：(.*?)\\n","agencyAddress");









        }


    }

    @Override
    public Site getSite() {
        return PageProcessor.super.getSite();
    }
}
